package pl.inea;

import pl.inea.config.KeyPreferences;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;

public class IneaMainScreen extends Activity implements OnClickListener {
    private final int REQUEST_CODE_LOGIN = 1;

    private ImageView imageViewPlay, buttonEpg, buttonBoa, buttonOptions;

    private SharedPreferences sharedPreferences;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.main);
	sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);

	imageViewPlay = (ImageView) findViewById(R.id.buttonPlay);
	imageViewPlay.setOnClickListener(this);

	buttonEpg = (ImageView) findViewById(R.id.buttonEPG);
	buttonEpg.setOnClickListener(this);

	buttonBoa = (ImageView) findViewById(R.id.buttonBOA);
	buttonBoa.setOnClickListener(this);

	buttonOptions = (ImageView) findViewById(R.id.buttonOptions);
	buttonOptions.setOnClickListener(this);
    }

    @Override
    protected void onResume() {
	super.onResume();
	checkIfUserIsLogged();
    }

    @Override
    public void onClick(View v) {
	int id = v.getId();
	Intent intent = null;

	switch (id) {
	case R.id.buttonPlay:
	    intent = new Intent(this, pl.inea.PlayScreen.class);
	    break;
	case R.id.buttonEPG:
	    intent = new Intent(this, pl.inea.EpgScreen.class);
	    break;
	case R.id.buttonBOA:
	    intent = new Intent(this, pl.inea.BOAScreen.class);
	    break;
	case R.id.buttonOptions:
	    intent = new Intent(this, pl.inea.OptionsScreen.class);
	    break;
	}

	if (intent != null)
	    startActivity(intent);
    }

    private void checkIfUserIsLogged() {
	if (!sharedPreferences.getBoolean(KeyPreferences.IS_USER_LOGGED_IN,
		KeyPreferences.IS_USER_LOGGED_IN_DEFAULT)) {
	    Intent intent = new Intent(this, pl.inea.LoginDialog.class);
	    startActivityForResult(intent, REQUEST_CODE_LOGIN);
		}
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
	super.onActivityResult(requestCode, resultCode, data);

	if (requestCode == REQUEST_CODE_LOGIN && resultCode != RESULT_OK)
	    finish();
    }

}